package io.github.talelin.latticy.controller.zty;


import io.github.talelin.core.annotation.LoginRequired;
import io.github.talelin.latticy.dto.zty.ShopListDTO;
import io.github.talelin.latticy.model.ztyDO.ShopStockDO;
import io.github.talelin.latticy.pojo.ReplenishDetail;
import io.github.talelin.latticy.pojo.ReplenishList;
import io.github.talelin.latticy.service.OutputListService;
import io.github.talelin.latticy.service.ReplenishListService;
import io.github.talelin.latticy.vo.UpdatedVO;
import io.github.talelin.latticy.vo.zty.ReplenishDetailVO;
import io.github.talelin.latticy.vo.zty.ReplenishListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Positive;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZTY
 * @since 2021-04-15
 */
@RestController
@RequestMapping("/replenish")
public class ReplenishListController {

    @Autowired
    ReplenishListService replenishListService;

    @Autowired
    OutputListService outputListService;

    @PostMapping("")
    @LoginRequired
    public void createReplenishApply(@RequestBody ShopListDTO shopListDTO) {
        System.out.println(shopListDTO);
        shopListDTO.getShopListDetails().forEach(System.out::println);
        int replenishList = replenishListService.createReplenishList(shopListDTO);
    }

    @GetMapping("/{id}")
    @LoginRequired
    public void getReplenishApplyDetail(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id){

    }

    @GetMapping("")
    @LoginRequired
    public List<ReplenishListVO> getReplenishList(){
        List<ReplenishListVO> replenishLists = replenishListService.getReplenishLists();
        replenishLists.forEach(System.out::println);
        return replenishLists;
    }

    @PutMapping("/pass/{id}/{handlerId}")
    @LoginRequired
    @Transactional
    public UpdatedVO passReplenishApply(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id, @PathVariable(value = "handlerId") @Positive(message = "{handlerId.positive}") Integer handlerId){
        System.out.println(handlerId);
        ReplenishList replenishList = replenishListService.getBaseMapper().selectById(id);
        replenishList.setStatus(2);
        int i = replenishListService.getBaseMapper().updateById(replenishList);
        outputListService.createOutputListByReplenishList(replenishList.getReplenishId(),handlerId);
        return new UpdatedVO(16);
    }

    @PutMapping("/reject/{id}")
    @LoginRequired
    public UpdatedVO rejectReplenishApply(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id){
        ReplenishList replenishList = replenishListService.getBaseMapper().selectById(id);
        replenishList.setStatus(3);
        int i = replenishListService.getBaseMapper().updateById(replenishList);
        return new UpdatedVO(17);
    }

    @GetMapping("/detail/{id}")
    @LoginRequired
    public List<ReplenishDetailVO>  getReplenishApplyDetailById(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id){
        System.out.println(id);
        List<ReplenishDetailVO> replenishDetails= replenishListService.getReplenishDetailsByListId(id);
        return replenishDetails;
    }

    @PutMapping("status/{applyListId}/{statusId}")
    @LoginRequired
    public UpdatedVO updateStatus(@PathVariable(value = "statusId") @Positive(message = "statusId.positive") Integer statusId,@PathVariable(value = "applyListId") @Positive(message = "applyListId.positive") Integer applyListId){
        ReplenishList replenishList = replenishListService.getBaseMapper().selectById(applyListId);
        replenishList.setStatus(statusId);
        int i = replenishListService.getBaseMapper().updateById(replenishList);
        UpdatedVO updatedVO;
        if (statusId == 2){
            updatedVO = new UpdatedVO(16);
        }else {
            updatedVO = new UpdatedVO(17);
        }
        return updatedVO;
    }
}

